//
//  StockNewsDetailController.m
//  Stock
//
//  Created by blacksun on 11-6-28.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockNewsDetailController.h"
#import "BaseStockChildListController.h"


@implementation StockNewsDetailController
@synthesize activeView;
@synthesize contents;
@synthesize tableView;
@synthesize stockNews;
@synthesize titleLabel;
@synthesize sourceLabel;
@synthesize pubDateLabel;
@synthesize stockNewsService;
@synthesize headerView;
@synthesize parent;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    ReadStatus *read = [[ReadStatus alloc] init];
    read.reportId = stockNews.reportId;
    read.readed = YES;
    [parent.readStatus addObject:read];
    [read release];
    
    self.contents = aResult;
    [self.tableView reloadData];
    [self.activeView stopAnimating];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}

- (void)dealloc
{   
    [stockNewsService cancleAllRequest];
    self.activeView = nil;
    self.tableView = nil;
    self.stockNews = nil;
    self.titleLabel = nil;
    self.sourceLabel = nil;
    self.pubDateLabel = nil;
    self.stockNewsService = nil;
    self.headerView = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    //titleLabel.textColor = [UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0];
    titleLabel.text = stockNews.title;
    sourceLabel.text = stockNews.source;
    pubDateLabel.text = stockNews.pubDate;
    self.stockNewsService = [[[StockNewsService alloc] initWithDelegate:self] autorelease];
    
    /*UIView *temp = [[UIView alloc] initWithFrame:CGRectMake(0.0, 59.0, 320.0, 0.5)];
    temp.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:temp];
    [temp release];
    */
    [headerView removeFromSuperview];
    self.tableView.tableHeaderView = headerView;
    if (!IsPaidVersion) {
        ADBannerView *banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 366.0, 320.0, 50.0)];
        [self.view addSubview:banner];
        [banner release];
        NSLog(@"ADBannerView");
    }
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [stockNewsService queryReportContentWithReportId:stockNews.reportId];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    [stockNewsService cancleAllRequest];
    self.activeView = nil;
    self.tableView = nil;
    self.contents = nil;
    self.titleLabel = nil;
    self.sourceLabel = nil;
    self.pubDateLabel = nil;
    self.stockNewsService = nil;
    self.headerView = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    return [contents count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    StockNewsContent *content = [contents objectAtIndex:indexPath.row];
    if ([kStockNewsContentTypeText isEqualToString:content.type]) {
        CGSize constraint = CGSizeMake(300.0, 20000.0);
		CGSize size = [content.content sizeWithFont:[UIFont systemFontOfSize:kStockNewsContentCellFontSize] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
		content.height = size.height;
        content.width = 300.0;
        return content.height+100.0; 
    }
    return 2000.0;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"StockNewsCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 10.0)];
        textLabel.tag = kStockNewsDetailCellTextTag;
        textLabel.font = [UIFont systemFontOfSize:kStockNewsContentCellFontSize];
        textLabel.textColor = [[[UIColor alloc] initWithRed:195.0/255.0 green:195.0/255.0 blue:195.0/255.0 alpha:1.0] autorelease];
        textLabel.backgroundColor = [UIColor clearColor];
		textLabel.numberOfLines = 0;
		textLabel.lineBreakMode = UILineBreakModeWordWrap;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 10.0)];
        imageView.tag = kStockNewsDetailCellImageTag;
        
        [cell.contentView addSubview:textLabel];
        [cell.contentView addSubview:imageView];
        
        [textLabel release];
        [imageView release];
    }
    UILabel *textLabel = (UILabel *)[cell viewWithTag:kStockNewsDetailCellTextTag];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:kStockNewsDetailCellImageTag];
    textLabel.hidden = YES;
    imageView.hidden = YES;
    StockNewsContent *content = [contents objectAtIndex:indexPath.row];
    if ([kStockNewsContentTypeText isEqualToString:content.type]) {
        textLabel.hidden = NO;
        textLabel.frame = CGRectMake(10.0, 50.0, content.width, content.height);
        textLabel.text = content.content;
    }
    return cell;
}
@end
